--	Package File Template
--
--	Purpose: This package defines supplemental types, subtypes, 
--		 constants, and functions 


library IEEE;
library STD;
use IEEE.STD_LOGIC_1164.all;
use IEEE.std_logic_textio.all;
use work.definitions.all;
use STD.textio.all;

package hdata is

 impure function loadMem (memAddress: integer) return std_logic_vector;
 impure function storeMem (memAdress:integer;data_word :std_logic_vector(7 downto 0)) return STD_LOGIC;
  
end hdata;

package body hdata is

 impure function loadMem (memAddress: integer) 
	return std_logic_vector is
	file mem_file_handle: TEXT;
	variable mem_line: line;
	variable mem_data: STD_LOGIC_VECTOR(7 downto 0);
 begin
	file_open(mem_file_handle,"C:\AIC\dataReg.txt",READ_MODE);
	--file_open(mem_file_handle,"C:\Users\Usuario\Desktop\PracticaAIC\dataReg.txt",READ_MODE);
	
	for i in 0 to 255 loop
		exit when i=memAddress;
	end loop;
	readline(mem_file_handle,mem_line);
	read(mem_line,mem_data);
	file_close(mem_file_handle);
	return mem_data;
 end loadMem;
 
 impure function storeMem (memAdress:integer;data_word :std_logic_vector(7 downto 0))
 return STD_LOGIC is
	file mem_file_handle: TEXT;
	variable mem_line: line;
	variable mem_data: STD_LOGIC_VECTOR(7 downto 0);
	type memory is array (0 to 255) of STD_LOGIC_VECTOR(7 downto 0);
	variable memoria:memory;
	variable aux:STD_LOGIC_VECTOR(7 downto 0);
 begin
 
	for i in 0 to 255 loop
		aux:=loadMem(i);
		memoria(i):=aux;
	end loop;

	file_open(mem_file_handle,"C:\AIC\dataReg.txt",WRITE_MODE);
	--file_open(mem_file_handle,"C:\Users\Usuario\Desktop\PracticaAIC\dataReg.txt",WRITE_MODE);

	for i in 0 to 255 loop
		write(mem_line,memoria(i)); 
		writeline (mem_file_handle,mem_line); 
	end loop;
	file_close(mem_file_handle);
	return '1';
	
end storeMem;

end package body;
